<?php
namespace app\admin\controller;
use think\Request;

class World extends Base
{
    
    // 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/world/login');
        }
        
        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }
            
            $result = db('customer')->where(['username' => $post['account']])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('该账号不存在');
            }
            
            if($result['status'] != 1) {
                err('账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('密码错误，请重新输入');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/world/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }
   
    
    ##球队列表
    public function user_list()
    {
        $post = request()->only(['type'],'post');
        
        if (empty($post['type'])){
            $where['group'] = "A";
        }else{
            $where['group'] = $post['type'];
        }
        
        $res = db('world_team')->where($where)->select();

        $this->assign('res', $res);
        return $this->fetch();
        
    }
    
    
    ##小组赛况
    public function group_list()
    {
        $res = db('world_cace')->where(['match'=>"小组赛"])->order('date asc')->select();
        
        $arr = db('world_team')->order('id asc')->select();

        foreach ($res as $k => $v){
            $res[$k]['img_a'] = "";
            $res[$k]['img_b'] = "";
            foreach ($arr as $key => $val){
                if ($v['team_id_a'] == $val['id']){
                    $res[$k]['team_id_a'] = $val['name'];
                    $res[$k]['img_a'] = $val['image'];
                }
                
                if ($v['team_id_b'] == $val['id']){
                    $res[$k]['team_id_b'] = $val['name'];
                    $res[$k]['img_b'] = $val['image'];
                }
                
                if ($v['victory'] == $val['id']){
                    $res[$k]['victory'] = $val['name'];
                }
            }
        }

        $this->assign('res', $res);
        return $this->fetch();
    }
    
    
    //修改小组战况
    public function group_list_edit($id)
    {
        $res = db('world_cace')->where(['id'=>$id])->order('date asc')->find();
        
        $arr = db('world_team')->order('id asc')->select();
        

        foreach ($arr as $key => $val){
            if ($res['team_id_a'] == $val['id']){
                $res['team_id_a'] = $val['name'];
                $res['img_a'] = $val['image'];
            }
            
            if ($res['team_id_b'] == $val['id']){
                $res['team_id_b'] = $val['name'];
                $res['img_b'] = $val['image'];
            }
        }


        $this->assign('arr', $arr);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    
    
    //修改小组赛
    public function group_edit_post()
    {
        $post = request()->only(['id','team_id_a','team_id_b','date','time','score','victory','statr','type'],'post');
        
        
        $res = db('world_cace')->where(['id'=>$post['id']])->update($post);
        
        if ($res) {
            $this->success('编辑成功','world/group_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    
    ##决赛赛况
    public function eight_list()
    {
        $post = request()->only(['type'],'post');
        
        if (empty($post['type'])){
            $post['type'] = "1/8决赛";
        }
        $res = db('world_cace')->where(['match'=>$post['type']])->order('date asc')->select();

        $arr = db('world_team')->order('id asc')->select();
    
        foreach ($res as $k => $v){
            $res[$k]['img_a'] = "";
            $res[$k]['img_b'] = "";
            foreach ($arr as $key => $val){

                if ($v['team_id_a'] == $val['id']){
                    $res[$k]['team_id_a'] = $val['name'];
                    $res[$k]['img_a'] = $val['image'];
                }
    
                if ($v['team_id_b'] == $val['id']){
                    $res[$k]['team_id_b'] = $val['name'];
                    $res[$k]['img_b'] = $val['image'];
                }
    
                if ($v['victory'] == $val['id']){
                    $res[$k]['victory'] = $val['name'];
                }
            }
        }

        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //修改决赛战况
    public function eight_list_edit($id)
    {
        $res = db('world_cace')->where(['id'=>$id])->order('date asc')->find();
    
        $arr = db('world_team')->order('id asc')->select();
    
    
        foreach ($arr as $key => $val){
            if ($res['team_id_a'] == $val['id']){
                $res['team_id_a'] = $val['name'];
                $res['img_a'] = $val['image'];
            }
    
            if ($res['team_id_b'] == $val['id']){
                $res['team_id_b'] = $val['name'];
                $res['img_b'] = $val['image'];
            }
        }
    
    
        $this->assign('arr', $arr);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    
    
    //修改决赛
    public function eight_edit_post()
    {
        $post = request()->only(['id','team_id_a','team_id_b','date','time','score','victory','statr','type'],'post');
    
    
        $res = db('world_cace')->where(['id'=>$post['id']])->update($post);
    
        if ($res) {
            $this->success('编辑成功','world/eight_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    
    
    
    
    
    ##积分排行
    public function  integral_list()
    {
        $res = db('world_ring')->order("type asc")->select();
        
        $arr = db('world_team')->order('id asc')->select();
        
        
        foreach ($res as $k => $v){
            $res[$k]['img'] = "";
            foreach ($arr as $key => $val){

                if ($v['name'] == $val['id']){
                    $res[$k]['name'] = $val['name'];
                    $res[$k]['img'] = $val['image'];
                }
    
            }
        }

        $this->assign('res', $res);
        return $this->fetch();
    }
     
    ##编辑积分页面
    public function integral_list_edit($id)
    {
        $res = db('world_ring')->where(['id'=>$id])->find();
        
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    ##编辑积分提交
    public function integral_edit_post()
    {
        $post = request()->only(['id','numbers','win','flat','negative','record','integral','sort'],'post');

        $res = db('world_ring')->where(['id'=>$post['id']])->update($post);

        if ($res) {
            $this->success('编辑成功','world/integral_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    
    ##配置页面
    public function config_list()
    {
        $res = db('configure')->where(['admin_id'=>$this->accountId])->field('share_title,share_img,home_page')->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    
    ##配置修改
    public function config_edit()
    {
        $post = request()->only(['share_title','home_page'],'post');
        
        //图片上传
        $file = new \app\admin\logic\File();
        $image = $file->uploadFileAdmin($this->accountId);
        
        if (!empty($image)){
            $post['share_img'] = $image;
        }
        
        
        $res = db('configure')->where(['admin_id'=>$this->accountId])->update($post);  
        
        if ($res) {
            $this->success('编辑成功','world/config_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    
    
    
    
    
    
    
    
    
    
}